/**
 * Regist Option Template
 * 
 * registOptions: {
 *	valid : {
 *		type : "post",
 *		url : "regist/validation",
 *		dataType : "json",
 *		data : {}
 *	},
 *	submit : {
 *		type : "post",
 *		url : "regist/submit",
 *		dataType : "json",
 *		data : {}
 *	}, 
 *
 *	beforeSend : function() {
 *	},
 *	validCallback : function(data) {
 *	},
 *	success : function() {
 *	},
 *	deny : function() {
 *	}
 * }
 * 
 * 
 * Login Option Template
 * 
 * loginOptions : {
 * 	valid : {
 *		type : "post",
 *		url : "login/validation",
 *		dataType : "json",
 *		data : {}
 *	},
 *	submit : {
 *		type : "post",
 *		url : "login/submit",
 *		dataType : "json",
 *		data : {}
 *	}, 
 *
 *	beforeSend : function() {
 *	},
 *	validCallback : function(data) {
 *	},
 *	success : function() {
 *	},
 *	deny : function() {
 *	}
 * }


*/
(function($) {
	
	var ajaxTemplate = function(options, beforeSend, success, error) {
		var temp = $.extend(options, {
			beforeSend : beforeSend,
			success : success,
			error : error
		});
		$.ajax(temp);
	};
	
	var call = function(func) {
		if(func)
			func.apply(this, Array.prototype.slice.call(arguments, 1));
	};

	var methods = {
		regist : function(options) {
			var validSettings = $.extend({
				type: "post",
				data: {},
				dataType: "json",
				url: "regist/validation"
			}, options.valid);
			var submitSettings = $.extend({
				type: "post",
				data: {},
				dataType: "json",
				url: "regist/submit"
			}, options.submit);
			
			ajaxTemplate(validSettings, function() {
				call(options.beforeSend);
				return true;
			}, function(valids) {
				if(valids.length != 0) {
					for(var i in valids) {
						var data = valids[i];
						call(options.validCallback, data);
					}
				} else {
					/* registration 부분 */
					ajaxTemplate(submitSettings, function() {
					}, function() {
						call(options.success);
					}, function() {
						call(options.deny);
					});
				}
			});

		},
		login : function(options) {
			var validSettings = $.extend({
				type: "post",
				data: {},
				dataType: "json",
				url: "login/validation"
			}, options.valid);
			var submitSettings = $.extend({
				type: "post",
				data: {},
				dataType: "json",
				url: "login/submit"
			}, options.submit);
			
			ajaxTemplate(validSettings, function() {
				call(options.beforeSend);
				
				return true;
			}, function(valids) {
				if(valids.length != 0) {
					for(var i in valids) {
						var data = valids[i];
						call(options.validCallback, data);
					}
				} else {
					/* registration 부분 */
					ajaxTemplate(submitSettings, function() {
					}, function() {
						call(options.success);
					}, function() {
						call(options.deny);
					});
				}
			});
		}
	};

	$.sso = function(method) {

		if(methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if( typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.sso');
		}

	};

})(jQuery);
